package com.example.administrator.familfinalcialmanagement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.bean.CommonUser;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.JaveTeacher;
import com.example.administrator.familfinalcialmanagement.bean.SuperManager;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMainBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.CommonUserDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.JaveTeacherDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.SuperManagerDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.MyInfoActivity;
import com.example.administrator.familfinalcialmanagement.ui.activity.RegisterActivity;
import com.example.administrator.familfinalcialmanagement.util.PermissionUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

import static com.example.administrator.familfinalcialmanagement.Constants.LOGINPRESSION;
import static com.example.administrator.familfinalcialmanagement.Constants.REQEEST_CDOE_GPS;

/**
 * 1 个人用户信息修改保存
 * 2 科目管理者信息展示修改保存
 * 3 各自的密码锁定
 */
public class MainActivity extends BaseActivity {

    ActivityMainBinding binding;

    CommonUserDao commonUserDao = GreenDaoUtils.getSingleTon().getmDaoSession().getCommonUserDao();
    JaveTeacherDao teacherDao = GreenDaoUtils.getSingleTon().getmDaoSession().getJaveTeacherDao();
    SuperManagerDao superManagerDao = GreenDaoUtils.getSingleTon().getmDaoSession().getSuperManagerDao();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        String isLogin = (String) SpUtils.get("isLogin", "");
        if ("1".equals(isLogin) || "2".equals(isLogin)) {
            startActivity(new Intent(MainActivity.this, RegisterActivity.class));
        } else if ("3".equals(isLogin)) {
            startActivity(new Intent(MainActivity.this, MyInfoActivity.class));
        }
        initPermission();
        initData();

        binding.tvModuleSuperUser.setOnClickListener(this);
        binding.tvModuleTeacher.setOnClickListener(this);
        binding.tvModuleCommonUser.setOnClickListener(this);
        binding.tvModuleAddTeacher.setOnClickListener( this );
        binding.tvModuleAddStudent.setOnClickListener( this );
    }

    private void initPermission() {
        if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
            if (!PermissionUtil.isOPen(MainActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, REQEEST_CDOE_GPS);
            }
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleAddStudent:
                Intent intentAddStudent = new Intent( MainActivity.this,AddReasionActivity.class );
                intentAddStudent.putExtra( "module_add_type","student" );
                startActivity( intentAddStudent );
                break;
            case R.id.tvModuleAddTeacher:
                Intent intentAddTeacher = new Intent( MainActivity.this,AddReasionActivity.class );
                intentAddTeacher.putExtra( "module_add_type","teacher" );
                startActivity( intentAddTeacher );
                break;
            //超级管理员登录
            case R.id.tvModuleSuperUser:
                String phone = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入您的账号");
                }

                String pwd = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                SpUtils.put("isLogin", "1");
                SpUtils.put("module_phone", phone);
                SpUtils.put("module_pwd", pwd);

                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                intent.putExtra("module_type", "1");
                intent.putExtra("module_phone", phone);
                intent.putExtra("module_pwd", pwd);
                startActivity(intent);

                break;
            //科目管理者登录
            case R.id.tvModuleTeacher:
                String phone1 = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone1)) {
                    ToastUtil.showToast("请输入您的账号");
                }

                String pwd1 = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd1)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                SpUtils.put("isLogin", "2");
                SpUtils.put("module_phone", phone1);
                SpUtils.put("module_pwd", pwd1);

                Intent intent1 = new Intent(MainActivity.this, RegisterActivity.class);
                intent1.putExtra("module_type", "2");
                intent1.putExtra("module_phone", phone1);
                intent1.putExtra("module_pwd", pwd1);
                startActivity(intent1);

                break;

            //普通用户登录
            case R.id.tvModuleCommonUser:
                String phone2 = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone2)) {
                    ToastUtil.showToast("请输入您的账号");
                }

                String pwd2 = binding.pwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd2)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                SpUtils.put("isLogin", "3");
                SpUtils.put("module_phone", phone2);
                SpUtils.put("module_pwd", pwd2);

                Intent intent2 = new Intent(MainActivity.this, MyInfoActivity.class);
                intent2.putExtra("module_type", "3");
                intent2.putExtra("module_phone", phone2);
                intent2.putExtra("module_pwd", pwd2);
                startActivity(intent2);

                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }

    /**
     * 初始化数据
     * 密码状态  1 锁定  2 非锁定
     */
    public void initData() {
        if (commonUserDao.loadAll().size() == 0) {
            CommonUser userOne = new CommonUser();
            userOne.setName("李明哲");
            userOne.setSex("男");
            userOne.setClassNameOne("JAVA");
            userOne.setClassOneNum("99");
            userOne.setClassNameTwo("PHP");
            userOne.setClassTwoNum("99");
            userOne.setClassNameThree("C");
            userOne.setClassThreeNum("99");
            userOne.setPhone("15515291700");
            userOne.setPwd("666666");
            userOne.setPwdStatus("2");

            CommonUser userTwo = new CommonUser();
            userTwo.setName("李信");
            userTwo.setSex("男");
            userTwo.setClassNameOne("JAVA");
            userTwo.setClassOneNum("98");
            userTwo.setClassNameTwo("PHP");
            userTwo.setClassTwoNum("97");
            userTwo.setClassNameThree("C");
            userTwo.setClassThreeNum("96");
            userTwo.setPhone("15515291701");
            userTwo.setPwd("666666");
            userTwo.setPwdStatus("2");

            CommonUser userThree = new CommonUser();
            userThree.setName("李白");
            userThree.setSex("男");
            userThree.setClassNameOne("JAVA");
            userThree.setClassOneNum("95");
            userThree.setClassNameTwo("PHP");
            userThree.setClassTwoNum("91");
            userThree.setClassNameThree("C");
            userThree.setClassThreeNum("89");
            userThree.setPhone("15515291702");
            userThree.setPwd("666666");
            userThree.setPwdStatus("2");

            CommonUser userFour = new CommonUser();
            userFour.setName("李晓丽");
            userFour.setSex("女");
            userFour.setClassNameOne("JAVA");
            userFour.setClassOneNum("97");
            userFour.setClassNameTwo("PHP");
            userFour.setClassTwoNum("96");
            userFour.setClassNameThree("C");
            userFour.setClassThreeNum("92");
            userFour.setPhone("15515291703");
            userFour.setPwd("666666");
            userFour.setPwdStatus("2");

            CommonUser userFive = new CommonUser();
            userFive.setName("李雷");
            userFive.setSex("男");
            userFive.setClassNameOne("JAVA");
            userFive.setClassOneNum("90");
            userFive.setClassNameTwo("PHP");
            userFive.setClassTwoNum("79");
            userFive.setClassNameThree("C");
            userFive.setClassThreeNum("89");
            userFive.setPhone("15515291704");
            userFive.setPwd("666666");
            userFive.setPwdStatus("2");
            commonUserDao.insert(userOne);
            commonUserDao.insert(userTwo);
            commonUserDao.insert(userThree);
            commonUserDao.insert(userFour);
            commonUserDao.insert(userFive);
        }

        /**
         *     public String name;
         *     public String phone;
         *     public String pwd;
         *     public String className;// JAVA  C  PHP
         */
        if (teacherDao.loadAll().size() == 0) {
            JaveTeacher javaTeacher = new JaveTeacher();
            javaTeacher.setName("欧阳哲桥");
            javaTeacher.setClassName("JAVA");
            javaTeacher.setPhone("15515291705");
            javaTeacher.setPwd("666666");
            javaTeacher.setOne("2");//密码状态  1 锁定  2 非锁定
            teacherDao.insert(javaTeacher);

            JaveTeacher phpTeacher = new JaveTeacher();
            phpTeacher.setName("欧阳名书");
            phpTeacher.setClassName("PHP");
            phpTeacher.setPhone("15515291706");
            phpTeacher.setPwd("666666");
            phpTeacher.setOne("2");//密码状态  1 锁定  2 非锁定
            teacherDao.insert(phpTeacher);

            JaveTeacher CTeacher = new JaveTeacher();
            CTeacher.setName("司马桥");
            CTeacher.setClassName("C");
            CTeacher.setPhone("15515291707");
            CTeacher.setPwd("666666");
            CTeacher.setOne("2");//密码状态  1 锁定  2 非锁定
            teacherDao.insert(CTeacher);
        }

        /**
         *     public String name;
         *     public String phone;
         *     public String pwd;
         */
        if (superManagerDao.loadAll().size() == 0) {
            SuperManager manager = new SuperManager();
            manager.setName("彭莉");
            manager.setPhone("17839577322");
            manager.setPwd("666666");
            superManagerDao.insert(manager);
        }
    }
}
